<template>
  <div class="lb-activity">
    <lb-title></lb-title>
    <div class="page-main">
      <lb-tip
        >{{ $t('menu.Activity') }}总数量: {{ totalNumber }}; 已分配数量:
        {{ useNumber }};</lb-tip
      >
      <el-row class="page-top-operate">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="centerDialogVisible = true"
          >新增授权</el-button
        >
        <!-- <el-button type='primary'>更新授权</el-button> -->
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="100px"> </el-table-column>
        <el-table-column prop="name" label="小程序名"> </el-table-column>
        <el-table-column prop="modular_id" label="小程序id"> </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="sign" label="到期时间">
          <template slot-scope="scope">
            <p>{{ scope.row.sign | handleTime(1) }}</p>
            <p>{{ scope.row.sign | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <el-button
                type="primary"
                size="mini"
                plain
                @click="addServiceTime(scope.row.modular_id)"
                >延长一年使用时间</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :page="pageForm.page"
        :pageSize="pageForm.page_count"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
      <el-dialog
        title="新增授权"
        :visible.sync="centerDialogVisible"
        width="400px"
        center
      >
        <div class="dialog-inner">
          <el-select
            v-model="value"
            filterable
            clearable
            placeholder="请选择授权的小程序"
          >
            <el-option
              v-for="item in miappOptions"
              :key="item.modular_id"
              :label="item.mini_name"
              :value="item.modular_id"
            >
            </el-option>
          </el-select>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addCompanyAuth">{{
            $t('action.comfirm')
          }}</el-button>
          <el-button @click="centerDialogVisible = false">{{
            $t('action.cancel')
          }}</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data () {
    return {
      serviceTips: `是否延长一年使用时间? 操作会消耗一个${this.$t('menu.Activity')}数量, 操作不可撤销`,
      pageForm: {
        page: 1,
        page_count: 10
      },
      loading: false,
      total: 0,
      tableData: [],
      centerDialogVisible: false,
      miappOptions: [],
      value: '',
      totalNumber: 0,
      useNumber: 0
    }
  },
  created () {
    this.getminiAppList()
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      this.$api.getAvtivityList(this.pageForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.totalNumber = res.data.total_activity_number
          this.useNumber = res.data.total_activity_used
          this.total = res.data.total
        }
      })
    },
    addCompanyAuth () {
      let { value } = this
      if (!value) {
        this.$message.error('请选择授权的小程序再提交！')
      } else {
        this.$api.addActivityMiniApp({ modular_id: value }).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successSub'))
            this.centerDialogVisible = false
            this.getTableDataList()
          }
        })
      }
    },
    addServiceTime (id) {
      this.$confirm(this.serviceTips, this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.$api.addActivityOneYear({ modular_id: id }).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successOper'))
            this.getTableDataList()
          }
        })
      })
    },
    getminiAppList () {
      this.$api.getAllMiniApp().then(res => {
        if (res.code === 200) {
          this.miappOptions = res.data
        }
      })
    },
    handleSizeChange (val) {
      this.pageForm.page_count = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.pageForm.page = val
      this.getTableDataList()
    }
  },
  watch: {
    centerDialogVisible (val) {
      if (val) {
        this.value = ''
      }
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-dialog {
  .el-select {
    width: 300px;
  }
}
.dialog-inner {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
